/*************************************************************************
	> File Name: callCase.c
	> Author: 
	> Mail: 
	> Created Time: Mon 10 Apr 2017 03:48:15 PM PDT
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
unsigned long pagetable[0x400*4]={0};
unsigned long *startStackPage = &pagetable[1024*4];

void fillPagetable(){
    int i;
    unsigned long stackbase =(unsigned long) &i;
    stackbase = stackbase >>12;
    pagetable[0]=0xFFFFFFFFFFFFF000;
    for(i=0x400;i<0x800;i++){
        pagetable[i]=0;
    };
    for(i=0xFFF;i>=0xC00;i--){
        pagetable[i] = stackbase - 0xFFF;
    }
    printf("stackbase = %lx",stackbase);
}

struct AA{
    int a;
    int (*cmpint)(int,int);
    int b;
};

struct AAA{
    struct AA * aa; 
};


int add(int a, int b){
    return a+b;
}

int comp(int a, int b, int (*com)(int,int)){
    int c = com(a,b);

decrease_c:
    c--;
    if(c<0){
        goto exit_func;
    }else{
        goto decrease_c;
    }

exit_func:
    printf("comp \n");
    return 0;
}

void callfuncTest(){
    int (*cmpint)(int,int);
    int result;
    cmpint = add;
    result = comp(3,4,cmpint); 
    printf("callfuncTest %d\n",result);
}

void structCallFuncTest(){
    int result;
    struct AA aa;
    struct AAA *aaa = malloc(sizeof(struct AAA));
    aaa->aa = &aa;
    aaa->aa->cmpint = add;
    result = comp(4,3,aaa->aa->cmpint);
    printf("structCallFuncTest %d\n",result);
}

void pointerTest(){
    int *temp;
    int i =1;
    temp = &i;
    i++;
    *temp = 5;
    printf("pointerTest = %d\n",i);
}

void inlinelocalvarTest(int b){
    int a = 5;
    int sum;
    int i;
    for(i=1;i<b;i++){
        int c =3;
        int *c_pr = &a;
        sum = *c_pr + c;
    }
    printf("inlinelocalvarTest = %d\n",sum);
}

int  main(){
    void (* p )(int b);
    fillPagetable();
    p=inlinelocalvarTest;

    callfuncTest();
    structCallFuncTest();
    pointerTest();
    p(3);
    return 0; 
}
